import React,{Component} from 'react';
import {
  Text,
  View,
  StyleSheet,
} from 'react-native';
import {
  Button,
  List,
  InputItem,
  Toast
} from 'antd-mobile';
const Item = List.Item;

export default class HomeComponent extends Component<{}>{


    static navigationOptions={
       title:'滴滴助手',
       headerStyle:{
         backgroundColor:'#fff',
       },
       headerTitleStyle:{
          color:'green',
          alignSelf : 'center',
       }
    }
    constructor(props){
       super(props)
       this.state={
         didi_lowest_byway_degree:'90',
         didi_route_lists:[],
         didi_suspense_list:[],
         data:''
       }
    }
    componentWillMount(){
       fetch('http://didi.spoplar.xyz/list',{
          method:'GET',
          headers: {
                'Content-Type': 'text/json'
          }
       })
       .then((response)=>response.text())
       .then((responseData)=>{
          var str = responseData.replace(/\'/g,'"');
          var jstr = JSON.parse(str)
          //Toast.success(jstr.didi_route_lists[0].strive_time);
          this.setState({
             didi_lowest_byway_degree:jstr.didi_lowest_byway_degree.toString(),
             didi_route_lists:jstr.didi_route_lists,
             didi_suspense_list:jstr.didi_suspense_list,
             data:jstr.didi_route_lists[0].strive_time
          })
       })
       .catch((error)=>{
         console.log(error);
       })
    }
    inputChange(value){
        fetch('http://didi.spoplar.xyz/update_lowest_byway_degree?degree='+value,{
          method:'GET',
          headers: {
                'Content-Type': 'text/json'
          }
        })
        .then((response)=>response.text())
        .then((responseData)=>{
           var str = responseData.replace(/\'/g,'"');
           var jstr = JSON.parse(str)
          if(jstr.code=='OK'){
             Toast.success(jstr.code);
              this.setState({
                 didi_lowest_byway_degree:jstr.didi_lowest_byway_degree.toString(),
              })
           }else{
             Toast.error(jstr.code);
           }
        })
        .catch((error)=>{
          console.log(error);
        })
    }

    render(){
      const {navigate} = this.props.navigation;

      return (
          <View>
            <List >
              <InputItem
               type="number"
               defaultValue={this.state.didi_lowest_byway_degree}
               editable={true}
               onChange={this.inputChange}
               >通知阀值</InputItem>
            </List>
            <List renderHeader={() => '临时行程' }>
              {
                this.state.didi_suspense_list.map((it)=>
                  <Item key={it.route_id} extra={it.departure_time} arrow='horizontal' wrap='true' onClick={()=>navigate('DetailComponent',{'route_id':it.route_id})}>{it.from_name}->{it.to_name}</Item>
                )
              }
            </List>
            <List renderHeader={() => '常规行程'}>
              {
                this.state.didi_route_lists.map((it)=>
                  <Item key={it.route_id} extra={it.departure_time} arrow='horizontal' wrap='true' onClick={()=>navigate('DetailComponent',{'route_id':it.route_id})}>{it.from_name}->{it.to_name}</Item>
                )
              }
            </List>
          </View>
      )
    }

}

const styles = StyleSheet.create({

});
